home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / ENET.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-30  |  3.6 KB  |  174 lines

  1. /* Stuff generic to all Ethernet controllers
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #ifdef ETHER
  6. #include "mbuf.h"
  7. #include "iface.h"
  8. #include "arp.h"
  9. #include "enet.h"
  10.  
  11. #if !defined(_lint)
  12. static char rcsid[] OPTIONAL = "$Id: enet.c,v 1.11 1997/07/31 00:44:20 root Exp root $";
  13. #endif
  14.  
  15. unsigned char Ether_bdcst[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  16.  
  17. static struct mbuf *htonether(struct ether *ether,struct mbuf *data);
  18.  
  19.  
  20.  
  21. /* Convert Ethernet header in host form to network mbuf */
  22. struct mbuf *
  23. htonether(
  24. struct ether *ether,
  25. struct mbuf *data
  26. ){
  27. char *cp;
  28. struct mbuf *bp;
  29.  
  30.     if(data == NULL || (bp = pushdown (data, ETHERLEN)) == NULLBUF)
  31.         return NULLBUF;
  32.  
  33.     cp = (char *) bp->data;
  34.  
  35.     memcpy (cp, ether->dest, EADDR_LEN);
  36.     cp += EADDR_LEN;
  37.     memcpy (cp, ether->source, EADDR_LEN);
  38.     cp += EADDR_LEN;
  39.     (void) put16 ((unsigned char *) cp, (int16) ether->type);
  40.  
  41.     return bp;
  42. }
  43.  
  44.  
  45.  
  46. /* Extract Ethernet header */
  47. int
  48. ntohether(
  49. struct ether *ether,
  50. struct mbuf **bpp
  51. ){
  52.     (void) pullup (bpp, ether->dest, EADDR_LEN);
  53.     (void) pullup (bpp, ether->source, EADDR_LEN);
  54.     ether->type = pull16 (bpp);
  55.     return ETHERLEN;
  56. }
  57.  
  58.  
  59.  
  60. /* Format an Ethernet address into a printable ascii string */
  61. char *
  62. pether(
  63. char *out,
  64. char *addr
  65. ){
  66.     sprintf(out,"%02x:%02x:%02x:%02x:%02x:%02x",
  67.      uchar(addr[0]),uchar(addr[1]),
  68.      uchar(addr[2]),uchar(addr[3]),
  69.      uchar(addr[4]),uchar(addr[5]));
  70.     return out;
  71. }
  72.  
  73.  
  74.  
  75. /* Convert an Ethernet address from Hex/ASCII to binary */
  76. int
  77. gether(
  78. register char *outorig,
  79. register const char *cp
  80. ){
  81. register int i;
  82. unsigned char *out = (unsigned char *) outorig;
  83.  
  84.     for (i = 6; i != 0; i--)    {
  85.         *out++ = uchar(htoi (cp));
  86.         if ((cp = strchr (cp, ':')) == NULL)    /* Find delimiter */
  87.             break;
  88.         cp++;            /* and skip over it */
  89.     }
  90.     return i;
  91. }
  92.  
  93.  
  94.  
  95. /* Send an IP datagram on Ethernet */
  96. int
  97. enet_send(
  98. struct mbuf *bp,    /* Buffer to send */
  99. struct iface *iface,    /* Pointer to interface control block */
  100. uint32 gateway,        /* IP address of next hop */
  101. int prec OPTIONAL,
  102. int del OPTIONAL,
  103. int tput OPTIONAL,
  104. int rel OPTIONAL
  105. ){
  106. char *egate;
  107.  
  108.     if (gateway == iface->broadcast) /* This is a broadcast IP datagram */
  109.         return (*iface->output) (iface, (char *) Ether_bdcst, iface->hwaddr, IP_TYPE, bp);
  110.  
  111.     egate = res_arp (iface, ARP_ETHER, gateway, bp);
  112.     if(egate != NULLCHAR)
  113.         return (*iface->output) (iface, egate, iface->hwaddr, IP_TYPE, bp);
  114.     return 0;
  115. }
  116.  
  117.  
  118.  
  119. /* Send a packet with Ethernet header */
  120. int
  121. enet_output(
  122. struct iface *iface,    /* Pointer to interface control block */
  123. const char *dest,    /* Destination Ethernet address */
  124. char *source,        /* Source Ethernet address */
  125. int16 type,        /* Type field */
  126. struct mbuf *data    /* Data field */
  127. ){
  128. struct ether ep;
  129. struct mbuf *bp;
  130.  
  131.     memcpy (ep.dest, dest, EADDR_LEN);
  132.     memcpy (ep.source, source, EADDR_LEN);
  133.     ep.type = type;
  134.     if ((bp = htonether(&ep,data)) == NULLBUF)    {
  135.         free_p (data);
  136.         return -1;
  137.     }
  138.     return (*iface->raw)(iface,bp);
  139. }
  140.  
  141.  
  142.  
  143. /* Process incoming Ethernet packets. Shared by all ethernet drivers. */
  144. void
  145. eproc(
  146. struct iface *iface,
  147. struct mbuf *bp
  148. ){
  149. struct ether hdr;
  150.  
  151.     /* Remove Ethernet header and kick packet upstairs */
  152.     (void) ntohether(&hdr,&bp);
  153.  
  154.     if(memcmp (hdr.dest, iface->hwaddr, EADDR_LEN) && memcmp (hdr.dest, Ether_bdcst, EADDR_LEN))    {
  155.         free_p(bp);
  156.         return;
  157.     }
  158.  
  159.     switch(hdr.type)    {
  160.         case REVARP_TYPE:
  161.         case ARP_TYPE:
  162.             arp_input (iface, bp);
  163.             break;
  164.         case IP_TYPE:
  165.             (void) ip_route (iface, bp, hdr.dest[0] & 1);
  166.             break;
  167.         default:
  168.             free_p (bp);
  169.             break;
  170.     }
  171. }
  172.  
  173. #endif        /* ETHER */
  174.